package homepage

import (
	"github.com/gin-gonic/gin"

	"go-papers/models"
	"go-papers/tools"
	"go-papers/tools/app"
)

func GetCourseList(c *gin.Context) {
	var data models.Course
	var err error
	var pageSize = 20
	var pageIndex = 1

	if size := c.Request.FormValue("pageSize"); size != "" {
		pageSize = tools.StrToInt(err, size)
	}
	if index := c.Request.FormValue("pageIndex"); index != "" {
		pageIndex = tools.StrToInt(err, index)
	}

	data.CourseTime = c.Request.FormValue("courseTime")

	ss := models.CourseSearch{}
	ss.CourseStatus = "5"
	ss.CourseType = c.Request.FormValue("courseType")
	ss.CourseName = c.Request.FormValue("courseName")
	ss.OrganName = c.Request.FormValue("organName")
	ss.BrandName = c.Request.FormValue("brandName")
	//授课方式 ：线上 、 线下
	ss.CourseTeach = c.Request.FormValue("courseTeach")
	ss.CourseExpensive = c.Request.FormValue("courseExpensive")
	//科目    courseCategory
	ss.CourseCategory = c.Request.FormValue("courseCategory")
	//距离   locationKm
	ss.LocationKm = c.Request.FormValue("locationKm")
	ss.LocationKmType = c.Request.FormValue("locationKmType")
	//年龄   courseAge
	ss.CourseAge = c.Request.FormValue("courseAge")
	//价格   priceStart   priceEnd
	//完成度  courseFinish
	ss.CourseFinish = c.Request.FormValue("courseFinish")

	//关键词
	ss.KeyWord = c.Request.FormValue("keyword")

	ss.MemberId = tools.GetMemberId(c)
	ss.OpenId = c.Request.FormValue("openId")
	ss.PriceStart = c.Request.FormValue("priceStart")
	ss.PriceEnd = c.Request.FormValue("priceStart")
	ss.PriceSort = c.Request.FormValue("priceSort")
	//
	var third models.ThirdUser
	ss.ThirdUserId, _ = third.GetIDByOpenId(ss.OpenId)

	result, count, err := data.GetPageSearch(pageSize, pageIndex, ss)
	if err != nil {
		tools.HasError(err, "网络异常，请稍后重试"+err.Error(), -1)
	}
	app.PageOK(c, result, count, pageIndex, pageSize, "")
}

func GetCourse(c *gin.Context) {
	var data models.Course
	data.CourseId, _ = tools.StringToInt(c.Param("id"))
	result, err := data.GetFromGateway()
	if err != nil {
		app.ApiResp(c, -2, "抱歉未找到相关信息："+err.Error(), nil)
		return
	}

	var organ models.Organ
	organInfo, _ := organ.GetByID(result.OrganId)
	result.OrganInfo = organInfo

	var bb models.OrganBrand
	brandInfo, _ := bb.GetByID(organInfo.OrganBrandId)
	result.BrandInfo = brandInfo

	result.OrganProvince = organInfo.OrganProvince
	result.OrganArea = organInfo.OrganArea
	result.OrganAddr = organInfo.OrganAddr
	result.OrganAddrTxt = organInfo.OrganAddrTxt
	result.OrganCity = organInfo.OrganCity
	result.OrganFeature = organInfo.OrganFeature
	result.OrganFeatureTxt = organInfo.OrganFeatureTxt
	// 查询用户是否收藏
	result.CollectStatus = "0"
	var collect models.MemberCollect
	collect.CourseId = result.CourseId
	collect.MemberId = tools.GetMemberId(c)
	cc, err := collect.Get()
	if err == nil && cc.CollectId > 0 {
		result.CollectStatus = "1"
	}

	// 查询是否在pk中
	result.ContrastStatus = "0"
	var cons models.MemberContrast
	cons.CourseId = result.CourseId
	cons.MemberId = tools.GetMemberId(c)
	c2, err := cons.Get()
	if err == nil && c2.ContrastId > 0 {
		result.ContrastStatus = "1"
		result.ContrastId = c2.ContrastId
	}

	app.OK(c, result, "")
}
